<?php
namespace app\farm\controller;
use app\AdminRest;
use app\ApiRest;

use app\farm\model\Config;
use app\farm\model\Coupon;
use app\farm\model\CouponRecord;
use app\farm\model\ShopOrder;
use app\farm\model\ShopOrderGoods;
use app\farm\model\ShopRefund;
use app\farm\model\Goods;
use app\farm\model\MsgConfig;
use app\farm\model\Order;
use app\farm\model\OrderAddress;
use app\farm\model\OrderGoods;
use app\farm\model\RefundOrder;
use app\farm\model\SystemInfo;
use app\shfarmop\model\User;
use longbingcore\wxcore\PushMsgModel;
use think\App;
use think\facade\Db;
use think\Request;


class AdminOrder extends AdminRest
{

    protected $model;

    protected $refund_model;

    protected $order_goods_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new ShopOrder();

        $this->refund_model = new ShopRefund();

        $this->order_goods_model = new ShopOrderGoods();

        autoCancelOrder($this->_uniacid);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 15:48
     * @功能说明:个人中心
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];

        }

        if(!empty($input['mobile'])){

            $dis[] = ['d.mobile','like','%'.$input['mobile'].'%'];

        }

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }

        if(!empty($input['store_id'])){

            $dis[] = ['a.store_id','=',$input['store_id']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        $data = $this->model->adminDataList($dis,$input['limit']);

        $arr = [
            //未支付
            'no_pay_count'   => 1,
            //未发货
            'no_send_count'  => 2,
            //已经发货
            'have_send_count'=> 3
        ];

        foreach ($arr as $ks=>$vs){

            $map = [

                'pay_type'=> $vs
            ];

            $data[$ks] = $this->model->where($map)->count();
        }

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:订单详情
     */
    public function orderInfo(){

        $input = $this->_param;


        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $data['over_time'] -= time();

        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

        $data['hx_time']     = date('Y-m-d H:i:s',$data['hx_time']);
        //剩余可申请退款数量
        $can_refund_num = array_sum(array_column($data['order_goods'],'can_refund_num'));
        //是否可以申请退款
        if(($data['pay_type']==7&&$data['can_refund_time']>time()&&$can_refund_num>0)||($data['pay_type']==2&&$can_refund_num>0)){

            $data['can_refund'] = 1;

        }else{

            $data['can_refund'] = 0;
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 17:29
     * @功能说明:退款订单详情
     *
     */
    public function refundOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $where = [];

        if(!empty($input['order_code'])){

            $where[] = ['a.order_code','like','%'.$input['order_code'].'%'];

            $where[] = ['d.order_code','like','%'.$input['order_code'].'%'];

        }

        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];

        }

//        if(!empty($input['order_code'])){
//
//            $dis[] = ['d.order_code','like','%'.$input['order_code'].'%'];
//
//        }

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        $data = $this->refund_model->indexDataList($dis,$where,$input['limit']);

        $map = [

            'status' => 1,

//            'user_id'=> $this->getUserId()
        ];
        //退款中数量
        $data['ing_count'] = $this->refund_model->where($map)->count();

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 17:50
     * @功能说明:退款订单详情
     */

    public function refundOrderInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->refund_model->dataInfo($dis);

        $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);

        $data['refund_time'] = date('Y-m-d H:i:s',$data['refund_time']);

        $order_model = new ShopOrder();

        $data['pay_order_code'] = $order_model->where(['id'=>$data['order_id']])->value('order_code');

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-25 17:14
     * @功能说明:楼长核销订单
     */
    public function endOrder(){

        $input = $this->_input;

        $order_model= new ShopOrder();

        $order = $order_model->dataInfo(['id'=>$input['id']]);

        if(empty($order)){

            $this->errorMsg('订单未找到');
        }

        if(empty($order)){

            $this->errorMsg('订单未找到');
        }
        //自提
        if($order['send_type']==1&&$order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }
        //快递
        if($order['send_type']==2&&$order['pay_type']!=3){

            $this->errorMsg('订单状态错误');
        }

        if($order['send_type']==1){

            $user_model = new \app\massage\model\User();

            $role =  $user_model->where(['id'=>$this->getUserId()])->value('role');

            if($role!=1){

                $this->errorMsg('只有管理员才能核销');
            }
        }

        $refund_model = new ShopRefund();
        //判断有无申请中的退款订单
        $refund_order = $refund_model->dataInfo(['order_id'=>$order['id'],'status'=>1]);

        if(!empty($refund_order)){

            $this->errorMsg('该订单正在申请退款，请先处理再核销');

        }

        $res = $order_model->hxOrder($input['id'],$this->getUserId());

        return $this->success($res);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-18 09:21
     * @功能说明:拒绝退款
     */
    public function noPassRefund(){

        $input = $this->_input;

        $refund_order_model = new ShopRefund();

        $res = $refund_order_model->noPassRefund($input['id'],$this->_user['id'],1);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**\
     * @author chenniang
     * @DataTime: 2021-03-18 09:28
     * @功能说明:同意退款
     */
    public function passRefund(){

        $input = $this->_input;

        $refund_order_model = new ShopRefund();

        $res = $refund_order_model->passOrder($input['id'],$input['price'],$this->payConfig(),$this->_user['id'],$input['text'],1);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-03-07 11:08
     * @功能说明:地主商城订单发货
     */
    public function shopOrderSend(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $order_model = new ShopOrder();

        $order = $order_model->dataInfo($dis);

        if(empty($order)||$order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }
        $refund_model = new ShopRefund();
        //判断有无申请中的退款订单
        $refund_order = $refund_model->dataInfo(['order_id'=>$input['id'],'status'=>1]);

        if(!empty($refund_order)){

            $this->errorMsg('该订单正在申请退款，请先处理再核销');

        }

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $update = [

            'pay_type'        => 3,

//            'express_company' => $input['express_company'],
//
//            'express_code'    => $input['express_code'],
//
//            'express_mobile'  => $input['express_mobile'],
//
//            'express_user'    => $input['express_user'],

            'send_time'       => time(),

            'hx_over_time'    => time()+$config['auto_hx_time']*86400,

        ];

        $data = $order_model->dataUpdate($dis,$update);

//        $water_model = new FinanceWater();
//        //商城订单农场主获得运费
//        $water_model->addWater($order['id'],16,1,0);

        $order = $order_model->dataInfo($dis);

        $order_model->sendOrderService($order);

        $sys_model = new PushMsgModel($order['uniacid']);

        $sys_model->sendMsg($order,4);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-29 15:27
     * @功能说明:上传视频
     */
    public function uploadVideo(){

        $input = $this->_input;

        $res = $this->model->dataUpdate(['id'=>$input['id']],['video'=>$input['video']]);

        return $this->success($res);

    }


}
